﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;

namespace SEP.Admin
{
    public partial class Parts : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindInfo();
            }

            ClientScript.RegisterStartupScript(this.GetType(), "PartModalPopup", "function showPartModal() { $find('ShowPartModal').show(); }", true);
        }

        private void BindInfo()
        {
            Common.Utility.BindGridView<Part>(grvParts, Part.GetAll());            
        }

        protected void btnNewPart_Click(object sender, EventArgs e)
        {
            pnlNewPart.Visible = true;
            modalNewPart.Show();
        }

        protected void btnPartSubmit_Click(object sender, EventArgs e)
        {
            string description = txtDescription.Text;

            if (!string.IsNullOrWhiteSpace(description))
            {
                Part part = new Part();
                part.Description = description;
                part.Number = txtNumber.Text;
                
                try
                {
                    part.Price = Convert.ToDecimal(txtPrice.Text);
                }
                catch{ }

                if (part.Create())
                {
                    txtDescription.Text = string.Empty;
                    txtNumber.Text = string.Empty;
                    txtPrice.Text = string.Empty;
                    lblPartError.Visible = false;
                }
                else
                {
                    lblPartError.Visible = true;
                    lblPartError.Text = SEPDefaults.ErrorMessages.GENERIC;
                }

                BindInfo();
            }
        }
    }
}